/**                            _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 */
package com.inossem.warehousemanage_tv;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.inossem.warehousemanage_tv.utils.crashException.CrashExceptionUtils;
import com.inossem.warehousemanage_tv.utils.logger.LogUtils;
import com.inossem.warehousemanage_tv.utils.logger.LogWriter;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.tencent.smtt.sdk.QbSdk;
import com.yatoooon.screenadaptation.ScreenAdapterTools;

/**
 * Application
 *
 * @author 詹建宇
 * @time on 2019/2/13 10:10
 * @email jianyu.zhan@inossem.com
 */
public class InossemApplication extends Application{
    private static InossemApplication context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;

        //初始化屏幕适配工具
        initDensityHelper();
        //logger
        initLogger();
        // 初始化异常捕获
        initCrashException(context);
    }

    private void initDensityHelper() {
        ScreenAdapterTools.init(this);
    }

    private void initLogger() {
        Logger.addLogAdapter(new AndroidLogAdapter());
    }

    private void initCrashException(Context context) {
        CrashExceptionUtils.initCrashException(context, (Context c) -> {
            LogWriter.saveLog(context, LogUtils.LOG_NAME_CRASHEXCEPTION, "未捕获异常造成崩溃，进入统一错误捕捉"
                    + LogUtils.NEW_LINE + LogUtils.exceptionToString(null));
            exitApp();
        });
    }

    private void initX5(){
        QbSdk.setDownloadWithoutWifi(true);
        //x5内核初始化接口//搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。
        QbSdk.initX5Environment(getApplicationContext(),  new QbSdk.PreInitCallback() {
            @Override
            public void onViewInitFinished(boolean arg0) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                Log.d("InossemApplication", " onViewInitFinished is " + arg0);
            }
            @Override
            public void onCoreInitFinished() {
            }
        });
    }

    //============================================public============================================

    /**
     * 获取Context
     */
    public static InossemApplication getContext() {
        return context;
    }

    /**
     * 退出程序
     */
    public void exitApp() {
        android.os.Process.killProcess(android.os.Process.myPid()); // 杀死进程
        System.exit(0);// 直接结束程序
    }

}
